from api.goods import get_list

class Goods:
    def __init__(self):
        self.reset()

    def get_list(self):
        response = get_list(self.page)
        if response['err'] == 0:
            if len(response['list']) > 0:
                self.list = self.list + response['list']
                self.page = self.page + 1
                self.handle_sku(response['list'])
            else:
                self.over = True
        else: 
            self.over = True
            self.error = True
    
    def get_all(self, callback=None):
        while self.over == False:
            self.get_list()
            if callback is not None:
                callback()

    def handle_sku(self, list):
        for item in list:
            for sku in item['sku'].values():
                # 加入列表
                self.sku.append({
                    "id": sku['id'],
                    "name": item["name"],
                    "item_ref": item["item_ref"],
                    "sku": sku['color_info']['name'],
                    "sn": sku['goods_sn'],
                    "pack": item['unit_number'],
                    "warehouse": sku['one_warehouse_0'],
                })    

    def reset(self):
        self.list = []
        self.sku = []
        self.page = 1
        self.over = False
        self.error = False